﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;

namespace StudentInfoSystem.UI.Views.Dialogs
{
    public partial class MessageDialog : Window
    {
        private DispatcherTimer _autoCloseTimer;

        public string Message
        {
            get => MessageText.Text;
            set => MessageText.Text = value;
        }

        public string IconText
        {
            get => MessageIcon.Text;
            set => MessageIcon.Text = value;
        }

        public Brush IconBrush
        {
            get => MessageIcon.Foreground;
            set => MessageIcon.Foreground = value;
        }

        public MessageDialog()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        // 辅助方法，设置对话框所有者
        private static void SetOwner(Window dialog, Window owner)
        {
            if (owner != null)
            {
                dialog.Owner = owner;
                dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            }
            else
            {
                dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            }
        }

        // 成功消息对话框
        public static void ShowSuccess(string message, string title = "操作成功",
                                       Window owner = null, int autoCloseSeconds = 0)
        {
            ShowMessage(message, title, "\uE73E", // CheckMark
                        GetBrush("SuccessBrush"), owner, autoCloseSeconds);
        }

        // 错误消息对话框
        public static void ShowError(string message, string title = "发生错误",
                                     Window owner = null, int autoCloseSeconds = 0)
        {
            ShowMessage(message, title, "\uE783", // ErrorBadge
                        GetBrush("ErrorBrush"), owner, autoCloseSeconds);
        }

        // 警告消息对话框
        public static void ShowWarning(string message, string title = "警告",
                                       Window owner = null, int autoCloseSeconds = 0)
        {
            ShowMessage(message, title, "\uE7BA", // Warning
                        GetBrush("WarningBrush"), owner, autoCloseSeconds);
        }

        // 信息消息对话框
        public static void ShowInfo(string message, string title = "信息",
                                    Window owner = null, int autoCloseSeconds = 0)
        {
            ShowMessage(message, title, "\uE946", // Info
                        GetBrush("InfoBrush"), owner, autoCloseSeconds);
        }

        // 核心显示方法
        private static void ShowMessage(string message, string title, string iconText,
                                        Brush iconBrush, Window owner, int autoCloseSeconds)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                var dialog = new MessageDialog
                {
                    Title = title,
                    Message = message,
                    IconText = iconText,
                    IconBrush = iconBrush
                };

                SetOwner(dialog, owner);

                // 设置自动关闭定时器
                if (autoCloseSeconds > 0)
                {
                    dialog.SetupAutoClose(autoCloseSeconds);
                }

                // 使用ShowDialog而不是Show，确保模态行为
                dialog.ShowDialog();
            });
        }

        // 设置自动关闭
        private void SetupAutoClose(int seconds)
        {
            _autoCloseTimer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(seconds)
            };

            _autoCloseTimer.Tick += (s, e) =>
            {
                _autoCloseTimer.Stop();
                DialogResult = true;
                Close();
            };

            _autoCloseTimer.Start();
        }

        // 重写OnClosed方法确保清理资源
        protected override void OnClosed(EventArgs e)
        {
            if (_autoCloseTimer != null)
            {
                _autoCloseTimer.Stop();
                _autoCloseTimer = null;
            }
            base.OnClosed(e);
        }

        // 辅助方法：从资源获取笔刷
        private static Brush GetBrush(string resourceName)
        {
            return Application.Current.TryFindResource(resourceName) as Brush
                   ?? Brushes.Black;
        }
    }
}